package com.bittech.client.service;

import com.bittech.client.dao.AccountDao;
import com.bittech.client.entity.Account;

import javax.swing.*;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/**
 * @Author: yuisama
 * @Date: 2019-06-04 17:33
 * @Description:
 */
public class UserLogin {
    private JPanel UserPanel;
    private JLabel Title;
    private JPanel AccountPanel;
    private JLabel userName;
    private JLabel password;
    private JButton regButton;
    private JButton loginButton;
    private JTextField userNameText;
    private JPasswordField passwordText;
    private AccountDao accountDao;
    private JFrame jframe;
    private Connect2Server connect2Server;

    {
        accountDao = new AccountDao();
    }

    public UserLogin() {
        // 登录事件
        // 成功进入用户列表界面
        // 失败弹出提示框
        loginButton.addActionListener( (e) -> {
                String userName = userNameText.getText();
                String password = String.valueOf(passwordText.getPassword());
                Account account = accountDao.login(userName,password);
                if (account != null) {
                    JOptionPane.showMessageDialog(null,"登录成功!","提示",
                            JOptionPane.INFORMATION_MESSAGE);
                    setFrameClosed();
                    // TODO 与Server建立通信，将自己Socket缓存到Server端并获取用户列表
                    connect2Server = new Connect2Server();
                    PrintStream out = new PrintStream(connect2Server.getClientWrite());
                    out.println("name:"+userName);
                    // 获取服务器缓存的所有用户名加载好友列表
                    Scanner in = new Scanner(connect2Server.getClientRead());
                    List<String> nameList = null;
                    if (in.hasNextLine()) {
                        String str = in.nextLine();
                        str = str.replace("[","");
                        str = str.replace("]","");
                        str = str.replace(" ","");
                        nameList = new ArrayList<>(Arrays.asList(str.split(",")));
                    }
                    System.out.println(nameList);
                    // 加载好友列表界面
                    UserFriendsList userFriendsList = new UserFriendsList(nameList,userName,connect2Server);
                }else {
                    JOptionPane.showMessageDialog(null,"用户不存在!","提示",
                            JOptionPane.ERROR_MESSAGE);
                }
        });
        // 注册事件
        // 成功注册返回登录界面登录
        // 失败回到登录界面重新注册
        regButton.addActionListener( (e) -> {
                UserReg userReg = new UserReg();
        });
    }


    public static void main(String[] args) {
        UserLogin userLogin = new UserLogin();
        userLogin.prepareGUI();
    }

    private void prepareGUI() {
        jframe = new JFrame("Login");
        jframe.setContentPane(UserPanel);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setLocationRelativeTo(null);
        jframe.pack();
        jframe.setVisible(true);
    }

    public void setFrameClosed() {
        jframe.setVisible(false);
    }

    public void setFrameOpen() {
        jframe.setVisible(true);
    }

    public JFrame getJframe() {
        return jframe;
    }
}
